package com.zhhtech.zhh.ui.adapter

import android.view.View
import com.zhhtech.zhh.ui.R
import com.zhhtech.zhh.ui.base.BaseRecyclerAdapter
import com.zhhtech.zhh.ui.databinding.ItemProductImageBinding
import com.zhhtech.zhh.ui.tool.image.select.service.IImageSelect
import com.zhhtech.zhh.ui.utils.GlideUtils

/**
 * 图片选择
 */
class UserImageAdapter(
    var value: Int = -1,
    private val defaultUpImage: Int = R.mipmap.icon_edit_user_default_up_image,
    private val imageSelect: IImageSelect? = null,
    private val changeCallback: (() -> Unit)? = null,
) : BaseRecyclerAdapter<ItemProductImageBinding, com.zhhtech.zhh.ui.tool.image.select.domain.ImageSelectBean>() {
    var maxLength: Int = 6
    private var upImageBean: com.zhhtech.zhh.ui.tool.image.select.domain.ImageSelectBean = com.zhhtech.zhh.ui.tool.image.select.domain.ImageSelectBean()
        .apply { resId = defaultUpImage }// 默认一个站位图

    init {
        newDatas(datas)
    }

    override fun newDatas(datas: MutableList<com.zhhtech.zhh.ui.tool.image.select.domain.ImageSelectBean>): MutableList<com.zhhtech.zhh.ui.tool.image.select.domain.ImageSelectBean> {
        if (datas.size < maxLength && !datas.contains(upImageBean)) datas.add(upImageBean)
        return datas
    }

    fun addImage(albumFiles: MutableList<com.zhhtech.zhh.ui.tool.image.select.domain.ImageSelectBean>) {
        if (albumFiles.isEmpty()) return
        val indexOf = datas.indexOf(upImageBean)
        datas.remove(upImageBean)
        notifyItemRemoved(indexOf)
        datas.addAll(albumFiles)
        val position = datas.size - 1
        if (datas.size < maxLength) datas.add(upImageBean)
        notifyItemRangeChanged(position, datas.size - position)
    }

    fun getOriginalSize(): Int {
        return getGalleryAlbums().size
    }

    fun getGalleryAlbums(): ArrayList<String> {
        val newDatas = ArrayList(datas).apply { remove(upImageBean) }
        val albumFiles: ArrayList<String> = ArrayList()
        for (item in newDatas) {
            albumFiles.add(item.path)
//            albumFiles.add(item.url ?: item.filePath ?: "")
        }
        return albumFiles
    }

    fun getFiles(): ArrayList<com.zhhtech.zhh.ui.tool.image.select.domain.ImageSelectBean> = ArrayList(datas).apply { remove(upImageBean) }

    override fun remove(position: Int) {
        super.remove(position)
        if (datas.size < maxLength && !datas.contains(upImageBean)) {
            datas.add(upImageBean)
            notifyItemInserted(datas.size - 1)
        }
    }

    override fun onBindViewHolder(vb: ItemProductImageBinding, bean: com.zhhtech.zhh.ui.tool.image.select.domain.ImageSelectBean, position: Int) {
        vb.run {
            if (bean.resId > 0) {
                GlideUtils.intoView(ivImage.context, bean.resId, ivImage)
            } else {
                GlideUtils.intoRoundedCorners(ivImage.context, bean.path, ivImage, roundingRadius = 10)
            }
//            if (bean.resId > 0) {
//                GlideUtils.intoView(ivImage.context, bean.filePath ?: bean.url ?: bean.resId, ivImage)
//            } else {
//                GlideUtils.intoRoundedCorners(ivImage.context, bean.filePath ?: bean.url ?: bean.resId, ivImage, roundingRadius = 10)
//            }
            tvDelete.visibility = if (bean.resId > 0) View.GONE else View.VISIBLE
            setViewClick(position, ivImage, tvDelete)
            setViewClickValue(value, ivImage, tvDelete)
        }
    }

    override fun subOnClickAdapter(view: View, position: Int) {
        if (imageSelect != null) {
            selectImage(view, position, imageSelect)
        }
    }

    fun selectImage(view: View, position: Int, imageSelect: IImageSelect?) {
        val item = getItem(position)
        when (view.id) {
            R.id.ivImage -> {
                if (item.resId > 0) {// 点击的是添加图片
                    imageSelect?.selectImage {
                        selectCount = maxLength - getOriginalSize()
                        onUploadSuccessListener = ::onUploadSuccess
                    }
                } else {
                    //mImageUploadUtils?.gallery(getGalleryAlbums(), position)
                }
            }
            R.id.tvDelete -> {
                remove(position)
            }
        }
    }

    private fun onUploadSuccess(imageUploads: MutableList<com.zhhtech.zhh.ui.tool.image.select.domain.ImageSelectBean>?) {
        imageUploads?.let {
            addImage(imageUploads)
            changeCallback?.invoke()
        }
    }
}